package com.goods.controller.business;

import com.goods.business.service.ProductCategoryService;
import com.goods.common.response.ResponseBean;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: 赵勇镔
 * @Time: 2021/6/7 17:21
 * @Description:
 */

//http://www.localhost:8989/business/productCategory/categoryTree?pageNum=1&pageSize=5
@Api(tags = "业务模块-物资类别")
@RestController
@RequestMapping("/business/productCategory")
public class ProductCategoryController {

    @Autowired
    private ProductCategoryService productCategoryService;


    @ApiOperation(value = "物资类别", notes = "物资类别展示")
    @GetMapping("/categoryTree")
    public ResponseBean<PageVO<ProductCategoryTreeNodeVO>> categoryTree(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                @RequestParam(value = "pageSize",defaultValue = "5") Integer pageSize) {
        PageVO<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOPageVO = productCategoryService.categoryTree(pageNum, pageSize);

        return ResponseBean.success(productCategoryTreeNodeVOPageVO);
    }


    @ApiOperation(value = "物资类别", notes = "父类回显")
    @GetMapping("/getParentCategoryTree")
    public ResponseBean<List<ProductCategoryTreeNodeVO>> getParentCategoryTre() {
        List<ProductCategoryTreeNodeVO> parentCategoryTre = productCategoryService.getParentCategoryTre();
        return ResponseBean.success(parentCategoryTre);
    }



    @ApiOperation(value = "物资类别", notes = "物资添加")
    @PostMapping("/add")
    public ResponseBean getParentCategoryTre(@RequestBody ProductCategoryTreeNodeVO productCategoryTreeNodeVO) {
        productCategoryService.add(productCategoryTreeNodeVO);
        return ResponseBean.success();
    }



    @ApiOperation(value = "物资类别", notes = "物资修改回显")
    @GetMapping("/edit/{id}")
    public ResponseBean getParentCategoryTre(@PathVariable Long id) {
        ProductCategoryTreeNodeVO productCategoryTreeNodeVO = productCategoryService.edit(id);
        return ResponseBean.success(productCategoryTreeNodeVO);
    }


    @ApiOperation(value = "物资类别", notes = "物资修改回显")
    @PutMapping("/update/{id}")
    public ResponseBean getParentCategoryTre(@PathVariable Long id,@RequestBody ProductCategoryTreeNodeVO productCategoryTreeNodeVO) {
        productCategoryService.update(id,productCategoryTreeNodeVO);
        return ResponseBean.success();
    }



    @ApiOperation(value = "物资类别", notes = "根据物资id删除")
    @DeleteMapping("/delete/{id}")
    public ResponseBean delete(@PathVariable Long id) {
        Boolean b = productCategoryService.delete(id);
        if(b){
            return ResponseBean.success();
        }else {
            Map<String,String> map = new HashMap<>();
            map.put("errorMsg","有子分类的、和物资引用的分类不能删除");
            return ResponseBean.error(map);
        }


    }



}
